package com.xl98.service.impl;

import com.xl98.entity.Permission;
import com.xl98.entity.Role;
import com.xl98.entity.RolePermission;
import com.xl98.mapper.PermissionMapper;
import com.xl98.mapper.RolePermissionMapper;
import com.xl98.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xl98.service.RolePermissionService;
import com.xl98.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
@Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
private PermissionMapper permissionMapper;
 @Autowired
 private RolePermissionService rolePermissionService;
    @Override
    public List<Permission> selectPermByRole(List<Role> roles) {
        List<Permission> permissions = new ArrayList<>();
        List<RolePermission> rolePermissions= new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        for (Role role : roles) {
            System.out.println(role.getId());
           rolePermissions.addAll(rolePermissionService.selectRolePermissionByRoleId(role.getId()));
        }
        for (RolePermission rolePermission : rolePermissions) {
            permissions.add(permissionMapper.selectById(rolePermission.getPermissionId()));
        }
        return permissions;
    }

    @Override
    public List<Permission> selectAllPerm() {
        return permissionMapper.selectList(null);
    }

    @Override
    public int addPerm(Permission permission) {
        return permissionMapper.insert(permission);
    }

    @Override
    public List<Permission> selectPermByRoleId(String roleId) {
        List<Permission> permissions = new ArrayList<>();
        List<RolePermission> rolePermissions= new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        rolePermissions.addAll(rolePermissionService.selectRolePermissionByRoleId(roleId));

        for (RolePermission rolePermission : rolePermissions) {
            permissions.add(permissionMapper.selectById(rolePermission.getPermissionId()));
        }
        return permissions;
    }
}
